package com.ems.mapper;

import com.ems.entity.UserEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ems.vo.UserVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 用户表 Mapper 接口
 * </p>
 *
 * @author WuZH
 * @since 2023-10-24
 */
@Mapper
public interface UserMapper extends BaseMapper<UserEntity> {

    @Select ( value = "SELECT *, u.name AS parentName FROM user_info ui LEFT JOIN role_info ri ON ui.roleId=ri.id LEFT JOIN user_info u ON ui.parentId=u.id LIMIT #{userVo.page}, #{userVo.limit}" )
    List<UserVo> queryList ( @Param ( "userVo" ) UserVo userVo );

    @Select ( value = "SELECT COUNT(1) FROM user_info ui LEFT JOIN role_info ri ON ui.roleId=ri.id LEFT JOIN user_info u ON ui.parentId=u.id" )
    Integer queryCount ( @Param ( "userVo" ) UserVo userVo );

    /**
     * 通过商户和角色加载员工列表
     *
     * @param userVo
     * @return
     */
    @Select ( value = "<script>SELECT * FROM user_info ui " +
            "<trim prefix=\"WHERE\" prefixOverrides=\"and | or\">" +
            "<if test='userVo.merchantId!=null'> AND ui.merchantId=#{userVo.merchantId} </if>" +
            "<if test='userVo.roleId!=null'> AND ui.roleId=#{userVo.roleId} </if>" +
            "<if test='userVo.parentId!=null'> AND ui.parentId=#{userVo.parentId} </if>" +
            "</trim></script>" )
    List<UserVo> getUserList ( @Param ( "userVo" ) UserVo userVo );

}
